(function () {


    var CreatDBSql = [
        // 选项表
        `CREATE TABLE option (
    key         CHAR(50)    PRIMARY KEY     NOT NULL,
    value       CHAR(50)                    NOT NULL
);`,
        // 用户表
        `CREATE TABLE user (
    user_id     CHAR(36)    PRIMARY KEY     NOT NULL,
    username    CHAR(16)                    NOT NULL,
    password    CHAR(16)                    NOT NULL,
    nickname    CHAR(8)                     NOT NULL,
    disable     INT(2)                      NOT NULL,
    update_time INT(14),
    create_time INT(14)
);`,
        // 链接服务表
        `CREATE TABLE service (
    service_id      CHAR(36)    PRIMARY KEY     NOT NULL,
    title           CHAR(16)                    NOT NULL,
    ip              CHAR(500)                   NOT NULL,
    username        CHAR(16)                    NOT NULL,
    password        CHAR(16)                    NOT NULL,
    update_time     INT(14),
    create_time     INT(14)
);`,
        // 项目表
        `CREATE TABLE project (
    project_id  CHAR(36)    PRIMARY KEY     NOT NULL,
    owner_user  CHAR(36)                    NOT NULL,
    title       CHAR(16)                    NOT NULL,
    description CHAR(500),
    update_time INT(14),
    create_time INT(14)
);`,
        // 文件夹表
        `CREATE TABLE folder (
    folder_id             CHAR(36)      PRIMARY KEY     NOT NULL,
    folder_project_id     CHAR(36)                      NOT NULL,
    folder_parent_id      CHAR(36),
    title                 CHAR(255)                     NOT NULL,
    type                  CHAR(255)                     NOT NULL,
    description           CHAR(1000),
    folder_index_array    CHAR(10000),
    child_index_array     CHAR(10000),
    disable               INT(2)                        NOT NULL,
    update_time           INT(14),
    create_time           INT(14)
);`,
        // 项目成员表
        `CREATE TABLE project_user (
    project_user_id     CHAR(36)      PRIMARY KEY   NOT NULL,
    project_id          CHAR(36)                    NOT NULL,
    user_id             CHAR(36)                    NOT NULL,
    update_time         INT(14),
    create_time         INT(14)
);`,
        // 项目URL表
        `CREATE TABLE project_url (
    url_id              CHAR(36)      PRIMARY KEY   NOT NULL,
    project_id          CHAR(36)                    NOT NULL,
    str                 CHAR(500)                   NOT NULL,
    title               CHAR(255)                   NOT NULL,
    disable             INT(2)                      NOT NULL,
    update_time         INT(14),
    create_time         INT(14)
);`,
        // 项目model表
        `CREATE TABLE project_model (
    model_id            CHAR(36)      PRIMARY KEY   NOT NULL,
    project_id          CHAR(36)                    NOT NULL,
    key                 CHAR(500)                   NOT NULL,
    name                CHAR(255)                   NOT NULL,
    text                CHAR(10000)                 NOT NULL,
    update_time         INT(14),
    create_time         INT(14)
);`,
        // 项目Api表
        `CREATE TABLE project_api (
    api_id          CHAR(36)      PRIMARY KEY     NOT NULL,
    project_id      CHAR(36)                      NOT NULL,
    folder_id       CHAR(36)                      NOT NULL,
    title           CHAR(225)                     NOT NULL,
    type            CHAR(10)                      NOT NULL,
    body_type       INT(2),
    disable         INT(2),
    status          INT(2),
    description     CHAR(200),   
    url             CHAR(255),
    header          CHAR(3000),
    body            CHAR(3000),
    fromdata        CHAR(3000),
    query           CHAR(3000),
    rest            CHAR(3000),
    before          CHAR(3000),
    after           CHAR(3000),
    update_time     INT(14),
    create_time     INT(14)
);`,
        // 项目功能表
        `CREATE TABLE project_function (
    function_id      CHAR(36)      PRIMARY KEY     NOT NULL,
    project_id       CHAR(36)                      NOT NULL,
    name             CHAR(225)                     NOT NULL,
    key              CHAR(225)                     NOT NULL,
    str              CHAR(10000)                   NOT NULL,
    testStr          CHAR(255),
    update_time      INT(14),
    create_time      INT(14)
);`,
        // 项目缓存表
        `CREATE TABLE project_cache (
    cache_id        CHAR(36)      PRIMARY KEY     NOT NULL,
    project_id      CHAR(36)                      NOT NULL,
    key             CHAR(225)                     NOT NULL,
    name            CHAR(225)                     NOT NULL,
    update_time     INT(14),
    create_time     INT(14)
);`,
        // 项目存储本地缓存表
        `CREATE TABLE local_api_cache (
    cache_id        CHAR(36)      PRIMARY KEY     NOT NULL,
    project_id      CHAR(36)                      NOT NULL,
    service_id      CHAR(36)                      NOT NULL,
    key             CHAR(255)                     NOT NULL,
    value           CHAR(255)                     NOT NULL,
    update_time     INT(14),
    create_time     INT(14)
);`,
        // 项目自定义Key值表
        `CREATE TABLE project_custom_key (
    custom_key_id   CHAR(36)      PRIMARY KEY     NOT NULL,
    project_id      CHAR(36)                      NOT NULL,
    key             CHAR(255),
    update_time     INT(14),
    create_time     INT(14)
)`
    ];

    var sqlite3 = require('sqlite3').verbose();
    var fs = require('fs');
    var file = './test.db';
    var haveDBStatus = true;

    if (!fs.existsSync(file)) {
        console.log("Creating db file!");
        haveDBStatus = false;
        fs.openSync(file, 'w');
    };


    window.db = {};
    var initDB = file => {
        return new Promise((resolve) => {
            db = new sqlite3.Database(file, function (err) {
                resolve(err);
            });
        })
    };

    var runDB = sql => {
        return new Promise((resolve, rejected) => {
            db.run(sql, function (err, res) {
                if (err) {
                    rejected(err);
                } else {
                    resolve(res);
                };
            });
        });
    };

    var allDB = sql => {
        return new Promise((resolve, rejected) => {
            db.all(sql, function (err, res) {
                if (err) {
                    rejected(err);
                } else {
                    resolve(res);
                };
            });
        });
    };

    var CreatDB = async () => {
        await initDB(file);
        for (let i = 0; i < CreatDBSql.length; i++) {
            const element = CreatDBSql[i];
            await runDB(element);
        };
        var initData = `insert into option values('version','3.0.0')`;
        await runDB(initData);


    };


    var initData = async () => {
        await initDB(file);
    };

    // var getAllOption = await allDB('SELECT * FROM option');
    // console.log(getAllOption);
    // var Option = {};
    // for (let index = 0; index < getAllOption.length; index++) {
    //     const element = getAllOption[index];
    //     Option[element.key] = element.value;
    // };
    // console.log(Option);


    // if (!haveDBStatus) {
    //     CreatDB();
    // };

    // initData();

})();